<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>
<%@ taglib prefix ="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html lang="en">
	<head>
		<%@ include file="/common/common-css.jsp"%>
		
		<link rel="stylesheet" href="/loan_manager/css/common-loading.css">
		
		<style>
			#jquery-table td {
				cursor: pointer !important
			}
			
			.productthumbnail {
				width: 100px;
				height: 100px;
			}
			
			@media (min-width: 768px) and (max-width: 1200px) {
				.backup-text {
					width: 100px !important;
				}
			}
		</style>
	</head>
	<body  class="no-skin">
		<%@ include file="/common/common-loading.jsp"%>
		<div class="page-content">
			<div class="page-header" style="margin-bottom:0px;padding-bottom:10px;">
				<h1 id="fu_name">
				</h1>
			</div>
			<form id="queryForm" method="post">
			<div class="page-content-area">
				<div class="row">
					<div class="col-sm-12">
						<div class="widget-box" style="border:0px solid #ccc; border-top:none;border-bottom:none;">
							<div class="widget-header widget-header-blue widget-header-flat">
								<h4 class="widget-title">商品列表</h4>
		
								<div class="widget-toolbar">
									<a href="#" data-action="collapse">
										<i class="ace-icon fa fa-chevron-up"></i>
									</a>
								</div>
							</div>
		
							<div class=" widget-body">
									<div class="widget-main" >
										
										<input type="hidden" name="sourceIds" id="sourceIds" placeholder=""
											class="col-xs-12 col-sm-6" value="${paramsMap.sourceIds}">
											
										<div class="form-group col-xs-12 col-sm-4 col-md-3 col-lg-3">
											<label class="col-sm-4 control-label"
												style="text-align: right" for="status">所属类目:</label>
											<div class="col-sm-8" style="padding-left: 0;">
												<select class="form-control" name="type">
													<option value="">全部</option>
												</select>
											</div>
										</div>
										
										<div class="form-group col-xs-12 col-sm-4 col-md-3 col-lg-3">
											<label class="col-sm-4 control-label"
												style="text-align: right" for="title">商品名称:</label>
											<div class="col-sm-8" style="padding-left: 0;">
												<input type="text" style="padding-left: 0px" name="title"
													id="title">
											</div>
										</div>
										
										<div class="form-group col-xs-12 col-sm-4 col-md-3 col-lg-3">
											<label class="col-sm-4 control-label"
												style="text-align: right" for="is_coupon_item">商品类型:</label>
											<div class="col-sm-8" style="padding-left: 0;">
												<select class="form-control" name="is_coupon_item">
													<option value="">请选择</option>
													<option value="0">返利商品</option>
													<option value="1">领券商品</option>
												</select>
											</div>
										</div>
										
									</div>
							</div>
						</div>
					</div>
						<div class="col-md-12 center">
						<div class="clearfix form-actions">
						
							<button class="btn btn-purple  no-border" type="button" name="btn-query" onclick="query_any()" id="btn-query">
								<i class="ace-icon fa fa-search icon-on-right bigger-110"></i>
								查询
							</button>
							
						  </div>
				   </div>
				</div>
		 		<div class="row">
					<div class="col-xs-12">
						<table id="jquery-table" class="table table-striped table-bordered table-hover">
						</table>
					</div>
				</div>
			</div>
			
			<br>
			<div class="col-md-12 center">
				    <div class="clearfix form-actions">
						<button class="btn btn-purple  no-border" type="button" name="btn-save" onclick="saveProduct()" id="btn-save">
								<i class="ace-icon fa fa-search icon-on-right bigger-110"></i>
								保存
						</button>
					</div>
			</div>
			
			</form>
		</div>
	</body>
</html>

<%@ include file="/common/common-js.jsp" %>
<!-- loading js -->
<script type="text/javascript" src="${ctx}/js/common-loading.js"></script>
<script type="text/javascript" src="${ctx}/js/call_func.js"></script>
<link rel="stylesheet" href="${ctx}/ace/css/just-tip.css"/>
<script src="${ctx}/ace/js/justTools.js"></script>

<!-- inline scripts related to this page -->
<script type="text/javascript">
	
	//追加勾选商品id的全局变量
	var selectedSourceIds = '';
		
    //【查询】按钮Click事件
	function query_any(){
	
		//获取在专题编辑页面已勾选商品的商品id
		var sourceIds = $("#sourceIds").val();
	  	selectedSourceIds = selectedSourceIds + sourceIds;
	  	
	 	$("#output").removeAttr("style");
		if(jqTable != null){
			jqTable.fnDraw();
		} else {
			loaddata();
		}
		 $("#delete_btn").attr("style","display:block");
		 $("#delete_hr").attr("style","margin-top:0px;margin-bottom:20px;display:block");
	 };
	
	 var jqTable = null;
	 var loaddata = function(){
		jQuery(function($) {
			jqTable = $('#jquery-table').dataTable( {
			    //数据列控制（包括对应返回JSON属性名称，对应表头列索引位置、数据转换封装等）
			    "columnDefs": [
			    	{
						  "title" : "<input type='checkbox' id='checkAll' onchange='selectAll(this)'/>",	
						  "name" : "position",
						  "data" : "source_id",
						  "targets" : [ 0 ],
						  "bSortable" : false,
						  "width": "1%",
						  "render" : function(value, type, row) { // 返回自定义内容
									return " <input onchange='myFunction(value, this)' type='checkbox' id='cbox' name='PK' value='"+value+"'/> ";
						  }
					},
					{	  
						  "title":'商品ID',
						  "name":"source_id",
						  "data": "source_id",
						  "targets": [1],
						  "class": "center",
						  "bSortable": false,
						  "width": "1%",
					},
					{	  
						  "title":'商品标题',
						  "name":"title",
						  "data": "title",
						  "targets": [2],
						  "class": "center",
						  "bSortable": false,
						  "width": "1%",
					},
					{	  
						  "title":'商品类型',
						  "name":"is_coupon_item",
						  "data": "is_coupon_item",
						  "targets": [3],
						  "class": "center",
						  "bSortable": false,
						  "width": "1%",
						  "render": function(value, type, row) {
							  switch(value){
								case 0:return "返利商品";
								case 1:return "领券商品";
								default : return "-";
							} 	
					      }
					},
					{	  
						  "title":'所属类目',
						  "name":"type",
						  "data": "type",
						  "targets": [4],
						  "class": "center",
						  "bSortable": false,
						  "width": "1%",
						  "render": function(value, type, row) {
						  		return '全部';
					      }
					},
			  	],
				"sAjaxSource": ctx+"/mallTopic/getOnlineItemList.action?rand=" + Math.random(),
				"fnServerData": function ( sSource, aoData, fnCallback ) {//从服务器请求数据
			   			var data = $('#queryForm').serializeObject();
						
						data.aoData = JSON.stringify(aoData);
						$.ajax({
							"type": "POST",
							"dataType": 'json',
							"url": sSource,
							"data":data,
							"timeout":300000,
							"beforeSend": function() {
								// 显示loading
								showLoading();
							},
							"success": function(resp){
								console.info(resp);
								hideLoading();
								fnCallback(resp);
								initListPage();
								
							},		
							"error": function(XMLHttpRequest, textStatus, errorThrown) {
								alert('请求服务器错误,请刷新重试！！');//错误提示，包括服务器重启导致的session失效
							},			
							"complete": function () {
								hideLoading();
							}
					});
				},
				
				
	  			"fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
	  				//console.log(nRow);
	  				//temp = nRow;
	  				if(selectedSourceIds != ''){
	  					var array= new Array();
	  					array = selectedSourceIds.split(",");
	  					for(i=0; i<array.length; i++){
	  						if(array[i] == aData.source_id){
	  							$(nRow).find('input').attr('checked', true);
	  						}
	  					}
	  				}
	  				
	  			},
				
				"sScrollXInner": "100%",
				"sScrollY": "100%",//如果不设置，IE会有问题
			    "aaSorting": [[2,"desc"]] //初始化加载，默认按第几列排序、排序方式（列从0开始计算）
			});
			
		});
	}
	
	$(document).ready(function() {
		//时间范围插件初始化
			$('.date-picker').datepicker({
				autoclose : true,
				format : 'yyyy-mm-dd'
			});
			
		$("#output").hide();
	});
	
	//勾选商品，追加商品id到selectedSourceIds字符串
	function myFunction(value, myCheckBox){
	
		if(myCheckBox.checked){
		  	//selectedSourceIds = selectedSourceIds + value + "," ;
		  	selectedSourceIds =  value + "," + selectedSourceIds;
		} else {
			selectedSourceIds = selectedSourceIds.replace(value + ",", '');
		}
		//console.log(selectedSourceIds);
	}
	
	//全选
	function selectAll(myAllCheckBox){
	
		//遍历页面，追加该页面全部商品id到selectedSourceIds字符串
		if(myAllCheckBox.checked){
			
			$("#jquery-table tr td:nth-child(2)").each(function () {
 		    	var source_id = $(this).text();
 		     	//防止重复追加在专题编辑页面已勾选的商品
 				if(selectedSourceIds.indexOf(source_id) < 0){
 			   		//selectedSourceIds = selectedSourceIds + source_id +",";
 			   		selectedSourceIds =  source_id + "," + selectedSourceIds;
 				}
			});
		
	   }else{
	     
	      selectedSourceIds = '';
	   
	   }
 		
	}
	
	
	//回传勾选的商品到专题页
	function saveProduct(){
      	parent.parent['__extdialog_sharedata'] = selectedSourceIds;
      	parent.parent['__extdialog_active'].dialog('close');
      	
	}
	
</script>